package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.OpenAreaDao;
import cn.edu.sdjzu.xg.bysj.domain.Degree;
import cn.edu.sdjzu.xg.bysj.domain.OpenArea;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;
import util.bysjException;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class OpenAreaService {
    private static OpenAreaService openAreaService = new OpenAreaService();
    private static OpenAreaDao openAreaDao = new OpenAreaDao();

    private OpenAreaService(){}

    public static OpenAreaService getInstance(){
        return openAreaService;
    }

    public Collection<OpenArea> findAll(
            List<Condition> conditionList_json_str, Pagination pagination)
            throws SQLException {
        //获得链接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<OpenArea> openAreas = openAreaDao.findAll(
                connection,conditionList_json_str,pagination);
        JdbcHelper.close(connection);
        return openAreas;
    }

    public OpenArea find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        OpenArea openArea = openAreaDao.find(id,connection);
        JdbcHelper.close(connection);
        return openArea;
    }

    public boolean update(OpenArea openArea) throws SQLException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = openAreaDao.update(openArea,connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean add(OpenArea openArea) throws SQLException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = openAreaDao.add(openArea,connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean delete(Integer id) throws SQLException, bysjException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = this.delete(this.find(id));
        JdbcHelper.close(connection);
        return result;
    }

    public boolean delete(OpenArea openArea) throws SQLException, bysjException {
        boolean result = false;
        Connection connection = JdbcHelper.getConn();
        result = openAreaDao.delete(openArea,connection);
        JdbcHelper.close(connection);
        return result;
    }
}
